package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.LoginUser;
import com.example.demo.commom.Result;
import com.example.demo.entity.Book;
import com.example.demo.entity.LendRecord;
import com.example.demo.entity.User;
import com.example.demo.mapper.BookMapper;
import com.example.demo.mapper.LendRecordMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/dashboard")
public class DashboardController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private LendRecordMapper lendRecordMapper;
    @Resource
    private BookMapper bookMapper;

    @GetMapping
    public Result<?> dashboardrecords() {
        // 获取访问次数
        int visitCount = LoginUser.getVisitCount();

        // 查询用户总数
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        Long userCount = userMapper.selectCount(userQueryWrapper);

        // 查询借阅记录总数
        QueryWrapper<LendRecord> lendRecordQueryWrapper = new QueryWrapper<>();
        Long lendRecordCount = lendRecordMapper.selectCount(lendRecordQueryWrapper);

        // 查询图书总数
        QueryWrapper<Book> bookQueryWrapper = new QueryWrapper<>();
        Long bookCount = bookMapper.selectCount(bookQueryWrapper);

        // 将数据放入 Map 中
        Map<String, Object> map = new HashMap<>();
        map.put("visitCount", visitCount);
        map.put("userCount", userCount);
        map.put("lendRecordCount", lendRecordCount);
        map.put("bookCount", bookCount);

        // 返回结果
        return Result.success(map);
    }
}
//这是一个典型的仪表盘控制器，用于提供系统的关键统计数据。
//
//它通过MyBatis-Plus高效地查询数据库，并将结果封装为统一的响应格式。
//
//通过模块化设计和简洁的代码，提高了可维护性和可读性。